import {Interceptors} from './interceptors';
import {message, Modal} from 'ant-design-vue';   // 弹吐司

const BASE_URL = '/api/v1'
export class HttpService {
    public axios: any;
    public modal: any;

    constructor() {
    	// 获取axios实例
        this.axios = new Interceptors(BASE_URL).getInterceptors();
    }

    /**
     * get请求
     * @param params  参数
     * @param jwt   是否token校验
     * @param modulename    模块
     * @param operation     接口
     * @param isLoad        是否要加载态
     * @param flag          标记
     * @param verson       版本,默认1.0.0
     * @param service      服务,默认services
     */
    public getData(params: object, modulename: string, operation: string,jwt: boolean,isLoad:boolean) {

        const url = modulename + '/' + operation;
        const body = {
            parameter: {
                data: params
            },
        };

        return new Promise((resolve, reject) => {
            this.axios.get(url, {
                params: body,
                headers: {isJwt: jwt,isLoad: isLoad},
            }).then((res:any) => {
                this.resultHandle(res, resolve);
            }).catch((err:any) => {
                reject(err.message);
            });
        });

    }


    /**
     * post请求
     * @param params  参数
     * @param jwt   是否token校验
     * @param modulename    模块
     * @param operation     接口
     * @param flag          标记
     * @param isLoad        是否要加载态
     * @param verson       版本,默认1.0.0
     * @param service      服务,默认services
     */
    public postData(params: object, modulename: string, operation: string, jwt: boolean, isLoad: boolean) {
        const url = modulename + '/' + operation;
        const body = params;
        return new Promise((resolve, reject) => {
            this.axios.post(url, body, {headers: {isJwt: jwt,isLoad: isLoad},
            }).then((res:any) => {
                this.resultHandle(res, resolve);
            }).catch((err:any) => {
                reject(err.message);
            });
        });

    }


    /**
     *
     * @param res
     * @param resolve
     */
    public resultHandle(res: any, resolve:any) {
        if (res.success) {
            resolve(res.data);
        } else {
            this.errorHandle(res);
        }
    }


    /**
     * 服务端状态处理,例如中断性异常,退出异常等等(与拦截器http握手状态注意区分,一般都能分清楚吧)
     * @param res
     */
    public errorHandle(res: any) {
        // 状态码判断
        switch (res.status) {
            case -102: 
                 break;
            case -152:
                break;
            default:
        }
    }

}